#!/usr/bin/python3
# -*- coding: utf-8 -*-

from pypinyin import lazy_pinyin, pinyin
import PySimpleGUI as sg
import pyperclip
import chardet

layout = [  [
                sg.Text('输入汉字：'),
                sg.Multiline(default_text='输入汉字',key='text_input', ),
                sg.Button('转换'),
                sg.Button('清空')
            ],
            [
                sg.Text('结果：', key='result'),
                sg.Multiline('',key='pinyin_output',background_color='#FFFFFF'),
                sg.Button('复制')
            ],

            [sg.Button('保存'), sg.Button('退出')] ]


if __name__ == '__main__':
    # Create the Window
    window = sg.Window('Window Title', layout, resizable=True, element_justification='center')
    # Event Loop to process "events" and get the "values" of the inputs
    while True:
        event, values = window.read()

        if event in (None, '退出'):  # if user closes window or clicks cancel
            break

        elif event == '转换':
            window['result'].update('结果：')
            text_list = pinyin(values['text_input'])
            pinyin_text_list = []
            for t in text_list:
                pinyin_text_list.append(''.join(t))
            pinyin_text = ' '.join(pinyin_text_list)
            pinyin_text.encode() # 只需要encode一下就可以了？为什么？
            window['pinyin_output'].update(pinyin_text)
            print(pinyin_text)

        elif event == '清空':
            window['text_input'].update('')
            window['pinyin_output'].update('')
            window['result'].update('结果：')

        elif event == '复制':
            output_text = window['pinyin_output'].get()
            output_text.strip()
            pyperclip.copy(output_text)
            window['result'].update('结果：复制成功！')

        elif event == '保存':
            pass
        elif event == '退出':
            pass
        # print('You entered ', values[0])
        print('event: ', event)

    window.close()
